<template>
  <div class="test-div__signin-main">
    <div class="test-div__signin-form">
      <el-form
        ref="formRef"
        :model="state.form"
        status-icon
        :rules="state.rules"
      >
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input
            v-model="state.form.username"
            autocomplete="off"
            placeholder="请输入账号"
            clearable
          />
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            v-model="state.form.password"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="signin"
            :loading="state.submitLoading"
          >登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import testTools from '@test/tools'

const router = useRouter()

const formRef = ref(null)

const state = reactive({
  rules: {
    username: [{ required: true, trigger: 'change', message: '请输入账号' }],
    password: [{ required: true, trigger: 'change', message: '请输入密码' }]
  },
  form: {
    username: '',
    password: ''
  },

  submitLoading: false
})

const signin = () => {
  formRef.value
    .validate()
    .then((valid) => {
      if (valid) {
        state.submitLoading = true

        setTimeout(() => {
          state.submitLoading = false
          if (state.form.username === 'admin' && state.form.password === '123456') {
            testTools.local.set('userInfo', {
              token: '123456',
              name: '超级管理员',
              authority: []
            })

            router.push('/home')
            ElMessage.success('登录成功')
          } else {
            state.form.password = ''
            ElMessage.error('账号或密码错误')
          }
        }, 500)
      }
    })
    .catch(() => {})
}

const reset = () => {
  formRef.value.resetFields()
}
</script>
  
  <style lang="less" scoped>
.test-div__signin-main {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .test-div__signin-form {
    border: 1px dotted #e3e3e3;
    padding: 30px;
  }
}
</style>
  